package com.zlp.common.core.erro;

import com.zlp.common.core.utils.Log;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zlp.common.base.BaseApiService;
import com.zlp.common.base.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @description: 全局捕获异常-->统一异常处理器
 * @author: ZouLiPing
 * @contact: 请关注微信公众号“给自己一个smile”里面有详细电商项目文档
 * @date: 2019年3月29日15:18:33
 * @version V1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler extends BaseApiService<JSONObject> {

	/**
	 * 运行时异常
	 * @param request
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public BaseResponse<JSONObject> exceptionHandler(HttpServletRequest request,Exception ex) {
		log.error("Runtime error api:{}, errcode:{}, msg:{}", Log.api(request.getRequestURI()), Log.errcode(500), Log.msg("运行时异常！"));
		log.error("Runtime error:{}, requestURL:{}", Log.exception(ex), Log.api(request.getRequestURI()));
		return setResultError(500,"系统内容错误");
	}

	/**
	 * 未知运行时异常
	 * @param request
	 * @param ex
	 * @return
	 */
	@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
	@ResponseBody
	public BaseResponse<JSONObject> handleException(HttpServletRequest request, Exception ex) {
		log.error("uncaught error api:{}, errcode:{}, msg:{}", Log.api(request.getRequestURI()), Log.errcode(500), Log.msg("内部错误"), Log.error());
		log.error("uncaught error ", Log.exception(ex), Log.api(request.getRequestURI()));
		log.error("uncaught error ", ex);
		return setResultError(500,"系统内部错误");
	}

	/**
	 * web服务器（容器）找不到service（）方法处理请求
	 * @param request
	 * @param ex
	 * @return
	 */
	@org.springframework.web.bind.annotation.ExceptionHandler(org.springframework.web.HttpMediaTypeNotAcceptableException.class)
	@ResponseBody
	public BaseResponse<JSONObject> handleHttpMediaTypeNotAcceptableException(HttpServletRequest request, Exception ex) {
		log.error("HttpMediaTypeNotAcceptableException uri:{}, code:{}, msg:{}", request.getRequestURI(), 405, ex.getMessage());
		log.error("HttpMediaTypeNotAcceptableException", ex);
		return setResultError(405,"web服务器（容器）找不到service（）");
	}

	@org.springframework.web.bind.annotation.ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	public BaseResponse<JSONObject> handleHttpRequestMethodNotSupportedException(HttpServletRequest request, Exception ex) {
		log.error("HttpMediaTypeNotAcceptableException uri:{}, code:{}, msg:{}", request.getRequestURI(), 405, ex.getMessage());
		log.error("HttpMediaTypeNotAcceptableException", ex);
		return setResultError(405,"web服务器（容器）找不到service（）");
	}

	/**
	 * 页面访问错误
	 * @param request
	 * @param ex
	 * @return
	 */
	@org.springframework.web.bind.annotation.ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class)
	@ResponseBody
	public BaseResponse<JSONObject> handleMissingServletRequestParameterException(HttpServletRequest request, Exception ex) {
		log.error("HttpMediaTypeNotAcceptableException uri:{}, code:{}, msg:{}", request.getRequestURI(), 405, ex.getMessage());
		log.error("HttpMediaTypeNotAcceptableException", ex);
		return setResultError(400,"页面访问错误！");
	}

	/**
	 * 自定义异常处理
	 * @param request
	 * @param ex
	 * @return
	 */
	@org.springframework.web.bind.annotation.ExceptionHandler(BusinessException.class)
	@ResponseBody
	public BaseResponse<JSONObject> handleBusinessException(HttpServletRequest request, Exception ex) {
		String uri = request.getRequestURI();
		BaseException e = (BaseException) ex;
		int code = e.getErrcode();
		String msg = e.getMsg();
		log.error("caught error api:{}, errcode:{}, msg:{}", Log.api(uri), Log.errcode(code), Log.msg(msg), Log.error());
		if (e.getException() != null) {
			log.error("caught error api:{}, error info", Log.exception(e.getException()));
		}
		return setResultError(code,msg);
	}



}
